<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use expand\Auth;
use think\response\Json;

/**
 * admin基础控制器
 * @author duqiu
 */
class Common extends AdminBase
{
    protected $m;   //当前控制器关联模型
    protected $v;

    /**
     * 基础控制器初始化
     * @author duqiu
     */
    protected function initialize()
    {
        parent::initialize();
        $userId = session('userId');
        if (!$userId) {//将请求的链接发送到一次性session中
            $this->redirect('/admin/login/index');
        }
        define('UID', $userId);//设置登陆用户ID常量
        define('MODULE_NAME', request()->module());
        define('CONTROLLER_NAME', request()->controller());
        define('ACTION_NAME', request()->action());
        self::Crumbs();
        $auth = new Auth();
        if (!$auth->check(CONTROLLER_NAME . '/' . ACTION_NAME, UID)) {
            if (request()->isAjax()) {
                $data = ['code' => 404, 'msg' => '当前没有权限，请联系管理员获取权限！', 'data' => null];
                echo json_encode($data);
                exit;
            } else {
                echo '403';exit;
//                $this->redirect('/403.html');
            }
        }
    }

    //获取面包屑,并将标题发送到页面
    public function Crumbs()
    {
        $request = request();
        if ($request->isGet()) {//若是get请求,并且是请求的方法是get‘index’则查询面包屑
            $authrule = \app\admin\model\AuthRule::where('name', CONTROLLER_NAME . '/' . ACTION_NAME)->find();
            if ($authrule) {
                \think\facade\View::share('html_title', $authrule['title']);//发送共享数据到模板里
                if (ACTION_NAME == 'index') {
                    $result = '<a><cite>' . $authrule['title'] . '</cite></a>';
                    $result = \app\admin\model\AuthRule::Crumbs($authrule, $result);
                    \think\facade\View::share('crumbs', '<a href="/admin/index/welcome">首页</a>' . $result);//发送共享数据到模板里
                }
            } else {
                \think\facade\View::share('html_title', '');//发送共享数据到模板里
                \think\facade\View::share('crumbs', '<a href="/admin/index/welcome">首页</a>');//发送共享数据到模板里
            }
        }
    }

    /**通用首页
     * @return mixed
     */
    protected function _index()
    {
        return $this->fetch();
    }

    /**通用添加数据
     * @return mixed|Response
     */
    protected function _add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            if ($this->v && !$this->v->scene('add')->check($data)) {
                return xjson(404, $this->m->getError());
            }
            $result = $this->m->create($data);
            if ($result) {
                return xjson();
            } else {
                return xjson(404, '添加失败！');
            }
        }
        return $this->fetch();
    }

    /**
     * 通用编辑数据
     * @param $id
     * @return mixed|Response
     */
    protected function _edit($id)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($this->v && !$this->v->scene('edit')->check($data)) {
                return xjson(404, $this->v->getError());
            }
            $this->m = $this->m->get($id);
            $result = $this->m->save($data);
            if ($result) {
                return xjson();
            } else {
                return xjson(404, '修改失败!');
            }
        } else {
            $data = $this->m->get($id);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    /**
     * ajax更新部分数据
     * @param $id
     * @return Response|Json
     */
    protected function _ajax($id)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($this->v && !$this->v->scene('ajax')->check($data)) {
                return json(['code' => 404, 'msg' => $this->v->getError(), 'data' => null]);
            }
            $m = $this->m->get($id);
            $m[key($data)] = $m[key($data)] ? 0 : 1;
            $result = $m->save();
            if ($result) {
                return xjson();
            } else {
                return xjson(404, '修改失败！');
            }
        } else {
            return xjson(403, '请求不被允许');
        }
    }

    /**
     * 通用删除
     * @param $id
     * @return Json
     */
    protected function _delete($id)
    {
        if (request()->isPost()) {
            $result = $this->m->where('id','in',$id)->delete();
            if ($result) {
                return xjson( '删除成功！');
            } else {
                return xjson(404, '删除失败！');
            }
        }
    }

}